﻿@{
    ViewBag.Title = "TestLib";
    Layout = "~/Views/Shared/_GridCommonLayout.cshtml";
}



<!--begin grid code-->


<section id="widget-grid" class="">
    <div>
        <h1 class=" txt-color-blueDark"><i class=" fa fa-file-text "></i>
            试题管理
               
        </h1>
    </div>

    <div class="heading-buttons" style="margin-left: 20px; margin-top: -15px;">

        <br />
        <div class="buttons pull-left">
            <span>
                <label>导入题型选择：</label>
                <select id="ImportDatatype" name="IProblemType" style="width: 80px">
                    <option value="1">请选择</option>
                    <option value="1">单选题</option>
                    <option value="2">多选题</option>
                    <option value="3">判断题</option>
                </select>
                <span>
                    <label id="bootstrapped-fine-uploader" style="width: 120px;"></label>
                    <label class="btn-group" style="margin-left: 10px; margin-top: 2px;">
                        <a href="javascript:void(0);" data-toggle="dropdown" class="btn btn-default dropdown-toggle" 
                            style="margin-top: 2px; height: 30px;">选导入模板<span class="caret"></span></a>

                        <ul class="dropdown-menu">
                            <li>
                                <a href="/Upload/TestModelFile/批量导入注意事项.doc">批量导入注意事项</a>
                            </li>
                            <li>
                                <a href="/Upload/TestModelFile/单选题导入模板.xls">单选题</a>
                            </li>
                            <li>
                                <a href="/Upload/TestModelFile/多选题导入模板.xls">多选题</a>
                            </li>
                            <li>
                                <a href="/Upload/TestModelFile/判断题导入模板.xls">判断题</a>
                            </li>
                        </ul>
                    </label>



                </span>
                <span id="HerfID" style="width: 35px; margin-left: 7px">
                    <a data-bind="attr:{href:HerfStr,target:'_blank'}" class="btn btn-success btn-labeled " role="button"><span class="btn-label"><i class="fa fa-download"></i>
                    </span>出错试题</a>
                </span>
            </span>



        </div>

        <div class="buttons pull-right">
            <button type="button" class="btn btn-success btn-labeled " onclick="location.href='/TeacherManger/Wizard?CourseID=@ViewBag.CourseID'">

                <span class="btn-label"><i class="glyphicon glyphicon-plus"></i>
                </span>添加新题</button>

            <button type="button" class="btn btn-primary btn-labeled "
                data-bind="click: $root.onDataDeleting">
                <span class="btn-label"><i class="glyphicon glyphicon-remove"></i>
                </span>批量删除</button>
        </div>
        <div style="clear: both;">
        </div>

    </div>
    <!--/Operation-->
    <!--Search-->
    <div class="filter-bar">
        <form id="formSearch">
            <div class="lbl glyphicons search" style="cursor: pointer;" data-bind="click: $root.search">
                <i></i>筛选
            </div>


            <div>
                <label>标题：</label><div class='input-append'>
                    <input id='txtbSrSTitle' name='txtbSrSTitle' type='text' placeholder='标题' class='input-mini' style='width: 120px;' />
                </div>
                <input type="hidden" value="@ViewBag.CourseId" id='txtbSrICousreId' name='txtbSrICousreId'>
            </div>
            <div>
                <label>对应大纲位置：</label>
                <select name="txtbSrSCharpter" id="txtbSrSCharpter" data-bind="event: { change: $root.myChange }">

                    <option value="">---请选择章---</option>
                    @foreach (var itemSel in ViewBag.Charpter)
                    {
                        <option value="@itemSel.SCaptailName" data-value="@itemSel.Id" >
                            @itemSel.SCaptailName</option> 
                    }

                </select>
                <select id="txtbSrITeaCatalog" name="txtbSrITeaCatalog" data-bind="options: arrayBindSesson,
                       optionsValue:'Id',
                       optionsText: 'SCaptailName',
                       optionsCaption: '---请选择节---'">
                </select>
            </div>
            <div>
                <label>试题权限：</label>
                <select id="txtbSrIStuOpen" name="txtbSrIStuOpen">
                    <option value="">--请选择类型--</option>
                    <option value="1">学生可见</option>
                    <option value="0">学生不可见</option>

                </select>
            </div>
            <div style="float: right">
                <label>题型选择：</label>
                <select id="IProblemType" name="IProblemType">
                    <option value="">请选择</option>
                    <option value="1">单项选择题</option>
                    <option value="2">多项选择</option>
                    <option value="3">判断题</option>
                </select>
            </div>
            <div class="clearfix">
            </div>

        </form>
    </div>
    <!--/search-->
    <article class="col-xs-12 col-sm-12 col-md-12 col-lg-12 sortable-grid ui-sortable">
        <div id="wid-id-0" class="jarviswidget jarviswidget-color-darken jarviswidget-sortable" data-widget-editbutton="false">
            <header>
                <span class="widget-icon"><i class="fa fa-table"></i></span>
                <h2>试题列表</h2>
            </header>
            <div style="margin-top: 35px;">
                <div class="jarviswidget-editbox"></div>
                <div class="widget-body no-padding">
                    <div role="grid" id="providerGrid" class="dataTables_wrapper form-inline">
                        <div class="table-responsive">

                            <div class="dt-wrapper" style="overflow-y: auto;">
                                <table class="table table-striped table-bordered table-hover checkboxs">
                                    <!--table head-->
                                    <thead>
                                        <tr>
                                            <th style="width: 1%">
                                                <input type="checkbox" value="0" />
                                            </th>
                                            <!-- ko foreach: headers -->
                                            <th class="left ">
                                                <a href="#" data-bind="text: displayText " style="color: black"></a>
                                                <i class="icon-circle-arrow-up" data-bind="    visible: value == $root.orderBy() && $root.isAsc()"></i>
                                                <i class="icon-circle-arrow-down" data-bind="    visible: value == $root.orderBy() && !$root.isAsc()"></i>
                                            </th>
                                            <!-- /ko -->
                                            <th class="center" style="width: 60px">操作
                                            </th>

                                        </tr>
                                    </thead>
                                    <!--/table head-->
                                    <!--table body(Provider list)-->
                                    <tbody data-bind="foreach: recordSet">
                                        <tr class="selectable" onclick="">
                                            <td class="center">
                                                <input type="checkbox" name="userSelector" data-bind="value: Id" />
                                                <input type="hidden" data-bind="value: Id, attr: { id: Id }" />
                                            </td>


                                            <td class='left'><strong data-bind='text: STitle'></strong></td>
                                            <td class='left'><strong data-bind='text: SCharpter'></strong></td>

                                            <td class='left'><strong data-bind='text: IHardLevel'></strong></td>
                                            <td class='left'><strong data-bind='text: $root.ChangeIStuOpen(IStuOpen)'></strong></td>

                                            <td class="center">

                                                <!--此处要用事件处理-->
                                                <a class="btn-action glyphicons pencil btn-success" data-bind="click:$root.showUpdateTestLibModal"><i></i></a>


                                            </td>

                                        </tr>
                                    </tbody>
                                    <!--/table body( Provider list)-->

                                </table>

                            </div>



                            <!-- 分页 -->
                            <ul class="pagination pagination-alt">
                                <li>
                                    <a data-bind="click: firstPage"><i class="fa fa-fast-backward "></i></a>
                                </li>
                                <li>
                                    <a data-bind="click: prevPage"><i class="fa  fa-backward  "></i></a>
                                </li>
                                <li>
                                    <a>
                                        <input id="toPage" type="text" data-bind="value: pageIndex, event: { change: $root.changePageIndex }" style="width: 25px;" />
                                        /共<span data-bind="html: totalPages"></span>页 
                                    </a>
                                </li>
                                <li>
                                    <a data-bind="click: nextPage"><i class="fa fa-forward "></i></a>
                                </li>
                                <li>
                                    <a data-bind="click: lastPage"><i class="fa fa-fast-forward "></i></a>
                                </li>
                            </ul>

                            <div class="dt-top-row total  ">
                                当前记录共：<span data-bind="html: totalCount"></span>条
                            </div>
                            <!--end 分页-->
                        </div>

                        <!-- /分页 -->


                    </div>

                    <!-- /分页 -->




                </div>
            </div>

        </div>




    </article>


    <!--end row-->
</section>
<input id="HiddenCourseID" type="hidden" value="@ViewBag.CourseID">

<script src="~/Scripts/Jcrop/jquery.fineuploader-3.1.min.js"></script>
<script type="text/javascript">

    var gdViewModel;
    $(function () {

        $("#HerfID").hide();
        $._messengerDefaults = {
            extraClasses: 'messenger-fixed messenger-theme-future messenger-on-bottom messenger-on-right'
        }; //默认提示位置;


        var options = {

            //dialogId: "TestLibMessageModal", //添加对话框


            dialogId: "previewMessageModal", //添加对话框


            pageSize: 20, //分页大小
            hidId: $("#txtbID"),
            SaveUpdateBtn: $("#btnSaveOrUpdateModel"),
            frmElement: $("#formTestLib"),

            //节点数据
            NodeValue: ko.observable({

                Id: 0,
                ICousreId: 0,
                SCourseCode: '',
                IProblemType: 0,
                STitle: '',
                SContent: '',
                IStuOpen: 0,
                SAnswer: '',
                ITeaCatalog: 0,
                IHardLevel: 0,
                IDiffLevel: 0,
                SPointers: '',
                SKeyWords: '',
                SCharpter: '',


            }),


            //用户列表Grid头部列名称
            headers: [

 { displayText: '标题', value: 'STitle', width: 'auto' },
 { displayText: '对应大纲位置', value: 'ITeaCatalog', width: 'auto' },
 { displayText: '难易度', value: 'IHardLevel', width: 'auto' },
 { displayText: '是否对学生公开', value: 'IStuOpen', width: 'auto' },
            ],

            //默认的排序方式
            defaultOrderBy: "Id",
            //用户查询URL
            dataQueryUrlAccessor: function () {
                return "/api/TestLibExt/DefaultSearch/";
            },

            //添加用户URL  ?id=
            dataAddUrlAccessor: function () { return "/api/TestLib/Post"; },
            dataUpdateAccessor: function (key) { return "/api/TestLib/Put/" + key; },
            dataNodeSelect: function (id) {
                //return "/api/TestLib/get?id=" + id;
                return "/api/TestLibExt/GetPreviewData?id=" + id;
            },

            dataDeleteAccessor: function (data) {
                var userIDs = "";
                var check = $("input:checked");  //得到所有被选中的checkbox
                check.each(function (i) {        //循环拼装被选中项的值
                    userIDs = userIDs + ',' + $(this).val();
                });
                //清除多余的分割号
                userIDs = dropRsplit(userIDs, ",");
                if (userIDs != "") {
                    return appendQueryString("/api/TestLibExt/DeleteTestLib/", { ids: userIDs });
                } else
                    return "";

            }
			, initEd: function () {
			    //gird初始化的后置事件 信息

			},



        };

        //点击“上传文件”，把选择的文件保存到指定的文件夹中
        $("#bootstrapped-fine-uploader").fineUploader({
            request: {

                endpoint: '/Api/TestLibExt/btnExcelUploadClick',
                params: {
                    ProblemType: function () {

                        return $("#ImportDatatype").val();;

                    },
                    CourseID: function () {
                        return $("#HiddenCourseID").val();
                    }
                },
            },

            text: {

                uploadButton: '<span class="btn-label"><i class="glyphicon glyphicon-open"></i></span> 导入试题'


            },
            validation:

                    {

                        allowedExtensions: ['xls'],

                        sizeLimit: 10485760 // 10MB = 10 * 1024kb * 1024 bytes

                    },
            template:

                   '<div class="qq-uploader span12">' +
                 '<pre class="qq-upload-drop-area span12"><span>{dragZoneText}</span></pre>' +
               '<div id="qq-upload-button" class="qq-upload-button btn btn-success btn-labeled" style="width: 100%; margin:5px;height:30px;">{uploadButtonText}</div>' +
                  '<span class="qq-drop-processing">' +
                     '<span class="qq-drop-processing-spinner"></span></span>' +
                 '<ul class="qq-upload-list" style="margin-top: 10px; text-align: center;display:none;"></ul>' +
                    '</div>',
            classes: {

                success: 'alert alert-success',

                fail: 'alert alert-error'

            },
            debug: true
        }).on('complete', function (event, id, fileName, responseJSON) {


            if (responseJSON.success) {
                alert(responseJSON.ReturnResult);

                if (responseJSON.Url != null) {//HrefID

                    $("#HerfID").show();
                    gdViewModel.HerfStr(responseJSON.Url);
                    gdViewModel.init();
                }
                else {
                    $("#HerfID").hide();
                }
            }

        })

        gdViewModel = new gridViewModel(options);

        gdViewModel.HerfStr = ko.observable();
        //转换函数，把题型数字转换成对应的字符串   
        gdViewModel.ChangeProblemType = function (IProblemType) {
            switch (IProblemType) {
                case 1:
                    return "单项选择题";
                    break;
                case 2:
                    return "多项选择题";
                    break;
                case 3:
                    return "判断题";
                    break;
                default:
                    break;
            }
        }
        gdViewModel.showUpdateTestLibModal = function (currUser) {
            var key = currUser.Id;
            var TypeKind = currUser.IProblemType;
            // 改变当前试题Session的Id
            $.ajax({
                url: '/api/TestLibExt/ChangeCurrentSessionId/',
                type: "POST",
                data: { id: key, kind: TypeKind },
                success: function () {
                    window.location.href = "/TeacherManger/UpdateTestLib?CourseID=" + $("#HiddenCourseID").val();
                }
            })


        };


        //9/1;所在节的处理
        gdViewModel.chaptesr = ko.observableArray();

        //在被选章里面绑定节
        gdViewModel.arrayBindSesson = ko.observableArray();
        gdViewModel.selectedchapter = ko.observableArray();//当前被选中的章
        gdViewModel.myChange = function (a, b, c, d) {//改变所选章时移动

            gdViewModel.arrayBindSesson.removeAll();//移除绑定的节

            var selV = $("#txtbSrSCharpter").find("option:selected").attr("data-value");
            if (selV == null || selV == "")
                return;
            $.getJSON("/api/TestLibExt/GetSession?ID=" + selV, null, function (data) {
                $.each(data, function (m, n) {

                    gdViewModel.arrayBindSesson.push(n);

                });

            });
        };

        //转换是否对学生公开的字符串
        gdViewModel.ChangeIStuOpen = function (IStuOpen) {
            if (IStuOpen == 0) {
                return "否";
            }
            return "是";
        }
        gdViewModel.init();
        ko.applyBindings(gdViewModel);

        options.frmElement.validate({
            rules: {
                txtbId: { required: true, digits: true },
                txtbICousreId: { digits: true },
                txtbSCourseCode: { maxlength: 50 },
                txtbIProblemType: { digits: true },
                txtbSTitle: { maxlength: 50 },
                txtbSContent: { maxlength: 50 },
                txtbIStuOpen: { digits: true },
                txtbSAnswer: { maxlength: 50 },
                txtbITeaCatalog: { digits: true },
                txtbIHardLevel: { digits: true },
                txtbIDiffLevel: { digits: true },
                txtbSPointers: { maxlength: 50 },
                txtbSKeyWords: { maxlength: 50 }
            },
            messages: {
                txtbId: { required: '必填项', digits: '必须是整数' },
                txtbICousreId: { digits: '必须是整数' },
                txtbSCourseCode: { maxlength: '最大长度为10' },
                txtbIProblemType: { digits: '必须是整数' },
                txtbSTitle: { maxlength: '最大长度为1000' },
                txtbSContent: { maxlength: '最大长度为0' },
                txtbIStuOpen: { digits: '必须是整数' },
                txtbSAnswer: { maxlength: '最大长度为0' },
                txtbITeaCatalog: { digits: '必须是整数' },
                txtbIHardLevel: { digits: '必须是整数' },
                txtbIDiffLevel: { digits: '必须是整数' },
                txtbSPointers: { maxlength: '最大长度为50' },
                txtbSKeyWords: { maxlength: '最大长度为100' }
            }
        });


    });

</script>


<!--/end grid code-->
